package com.student.controller;

import com.student.pojo.Cj;
import com.student.pojo.Ke;
import com.student.pojo.Student;
import com.student.service.CjService;
import com.student.service.ClassesService;
import com.student.service.KeService;
import com.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.dsig.keyinfo.KeyName;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author 是一个鸽子啊
 * @date 2020/4/14
 * @qq 364826415
 */
@Controller
public class CjController {
    @Autowired
    @Qualifier("cjServiceImpl")
    CjService cjService;
    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;
    @Autowired
    @Qualifier("classesServiceImpl")
    private ClassesService classesService;
    @Autowired
    @Qualifier("KeServiceImpl")
    private KeService keService;
    @RequestMapping("/queryCjList")
    private String queryCjList(HttpServletRequest request){
        List<Cj> cjList = cjService.queryCjList();
        for (Cj cj : cjList) {
            cj.setStudent(studentService.queryStudentByID(cj.getCjSID()));
            cj.setClasses(classesService.queryClassesByID(cj.getStudent().getStudentCID()));
            cj.setKe(keService.queryKeByID(cj.getCjKID()));
        }
        request.setAttribute("cjList",cjList);
        return "/grade-table";
    }
    @RequestMapping("/updateCjByID")
    private String updateCjByID(Cj cj,HttpServletRequest request){
        cjService.updateCjByID(cj);
        List<Cj> cjList = cjService.queryCjList();
        for (Cj cj1 : cjList) {
            cj1.setStudent(studentService.queryStudentByID(cj1.getCjSID()));
            cj1.setClasses(classesService.queryClassesByID(cj1.getStudent().getStudentCID()));
            cj1.setKe(keService.queryKeByID(cj1.getCjKID()));
        }
        request.setAttribute("cjList",cjList);
        return "/grade-table";
    }
    @RequestMapping("/deleteCjByID")
    private String deleteCjByID(String cjID,HttpServletRequest request){
        cjService.deleteCjByID(Integer.parseInt(cjID));
        List<Cj> cjList = cjService.queryCjList();
        for (Cj cj1 : cjList) {
            cj1.setStudent(studentService.queryStudentByID(cj1.getCjSID()));
            cj1.setClasses(classesService.queryClassesByID(cj1.getStudent().getStudentCID()));
            cj1.setKe(keService.queryKeByID(cj1.getCjKID()));
        }
        request.setAttribute("cjList",cjList);
        return "/grade-table";
    }
    @RequestMapping("/queryCJByStudentName")
    private String queryCJByStudentName(String studentName,String keName,HttpServletRequest request) {
        List<Cj> cjList = cjService.queryCjList();
        List<Cj> cjList1=new ArrayList<Cj>();
        for (Cj cj : cjList) {
            cj.setStudent(studentService.queryStudentByID(cj.getCjSID()));
            cj.setClasses(classesService.queryClassesByID(cj.getStudent().getStudentCID()));
            cj.setKe(keService.queryKeByID(cj.getCjKID()));
        }
        for (Cj cj : cjList) {
            if (cj.getStudent().getStudentName().contains(studentName)&&cj.getKe().getKeName().contains(keName)){
                cjList1.add(cj);
            }
        }
        System.out.println(cjList1);
        request.setAttribute("cjList",cjList1);
        return "/grade-table";
    }
    @RequestMapping("/addCj")
    private String addCj(Cj cj,HttpServletRequest request){
        cjService.addCj(cj);
        Student student = studentService.queryStudentByID(cj.getCjSID());
        request.setAttribute("msg",student.getStudentName()+"的成绩添加成功!");
        return "/grade-add";
    }
}
